home *** CD-ROM | disk | FTP | other *** search
/ Belgian Amiga Club - ADF Collection / BS1 part 26.zip / BS1 part 26 / Macro assembler dev syst.adf / ovs.asm < prev    next >
Assembly Source File  |  1985-12-20  |  13KB  |  388 lines

  1.         TTL     AmigaDOS 68000 overlay supervisor
  2. **********************************************************
  3. *                                                        *
  4. * This version of the overlay supervisor is callable     *
  5. * from C or Assembler; it will not work for MCC Pascal   *
  6. * or BCPL which use a calling sequence where the called  *
  7. * routine's address is saved on the stack. A different   *
  8. * version of the overlay supervisor is available for     *
  9. * case.                             *
  10. *                             *
  11. * The main overlay algorithm used here was originally    *
  12. * described by Richard Evans at Cambridge University     *
  13. * in about 1979. This code implementation was done by    *
  14. * Tim King of Metacomco.                 *
  15. *                                                        *
  16. * Two main data structures are used:                     *
  17. *                                                        *
  18. *   1. The hunk table                                    *
  19. *                                                        *
  20. *        This table, giving the base addresses of all    *
  21. *          hunks currently loaded, is manufactured by    *
  22. *          the loader.  Its address is planted in HTAB,  *
  23. *          and its first word is the address of hunk 0.  *
  24. *          Note that the addresses it contains are those *
  25. *          of the link word of each hunk - not the first *
  26. *          data word.                                    *
  27. *                                                        *
  28. *   2. The overlay table                                 *
  29. *                                                        *
  30. *        This table is manufactured by the linkage       *
  31. *          editor, and its address is planted in OVTAB   *
  32. *          by the loader.   It consists of two main      *
  33. *          parts, the ordinate table, and the overlay    *
  34. *          data table.  The former is a table which is   *
  35. *          used to remember the ordinate number of each  *
  36. *          segment that is currently loaded. If n is the *
  37. *          maximum level number, then the table contains *
  38. *          n+1 entries, and the first word is n+2. The   *
  39. *          extra entry is required because a scan        *
  40. *          through the table below is terminated by a    *
  41. *          zero entry.  All the entries are initially    *
  42. *          cleared by the linkage editor.  The address   *
  43. *          of the overlay table, plus the contents of    *
  44. *          its first word (n+2), gives the address of    *
  45. *          the overlay data table, which is indexed by   *
  46. *          overlay number.  Each entry in this table     *
  47. *          contains the following information, not       *
  48. *          necessarily in this order:                    *
  49. *                                                        *
  50. *            1. A file mark, generated by 'note', giving *
  51. *                the position of the segment to be       *
  52. *                loaded.                                 *
  53. *            2. The overlay level of the segment.        *
  54. *            3. The overlay ordinate of the segment.     *
  55. *            4. The initial hunk number of the segment.  *
  56. *            5. The number of the hunk containing the    *
  57. *                symbol.                                 *
  58. *            6. The offset of the symbol in the hunk.    *
  59. *                                                        *
  60. * If an error is detected during overlaying, there is a  *
  61. * limited choice of sensible actions, since the overlay  *
  62. * supervisor can make no assumptions concerning the      *
  63. * environment it was entered from.  The action on an     *
  64. * error is to call an Alert with the code for invalid    *
  65. * overlay request.                     *
  66. *                                                        *
  67. * Note that the value in HTAB is a long word address,    *
  68. * whilst that in OVTAB is a machine address.  Each entry *
  69. * in the table is four bytes long.                       *
  70. *                                                        *
  71. **********************************************************
  72. *  Change History:
  73. *
  74. *    CARL 05SEP85
  75. *         1)    Changes made (suggested by Paul) to the handling
  76. *        of seek and loadseg errors.  When an error
  77. *        occurs, the program will retry until it is
  78. *        successful.
  79. *         2)    Changed bra.s to just bra in OVENT and OVENT2
  80. *        macro definitions.
  81. *         3)    Added the OVERLAYS equate and the necessary OVENT2
  82. *        table entries (conditionally assembled).
  83. *         4) Eliminated unnecessary include files.
  84. *    CARL 06SEP85
  85. *         1) CALL macro modified.
  86. *         2) CALL use modified.
  87.  
  88.  
  89. OVERLAYS    EQU    20    number of overlays handled
  90.  
  91.  
  92.         RORG       0                    Relocatable code
  93.  
  94.  
  95.     INCLUDE "exec/types.i"
  96.     INCLUDE "exec/tasks.i"
  97.     INCLUDE "exec/libraries.i"
  98.     INCLUDE "exec/alerts.i"
  99.     INCLUDE "libraries/dos.i"
  100.  
  101. * XREFS (Not allowed)
  102. *    xref    _LVOAlert
  103. *    xref    _LVOFreeMem
  104. *    xref    _LVOSeek
  105. *    xref    _LVOOpenLibrary
  106. *    xref    _LVOLoadSeg
  107.  
  108. * Kludge to determine the correct LVO XREF's:
  109. FUNCDEF        MACRO    * function
  110. _LVO\1        EQU    FUNC_CNT
  111. FUNC_CNT    SET    FUNC_CNT-6
  112.         ENDM
  113.  
  114. FUNC_CNT    SET    LIB_NONSTD
  115.  
  116.     INCLUDE    "exec/exec_lib.i"
  117.     INCLUDE    "libraries/dos_lib.i"
  118.  
  119.  
  120. * Now declare some useful manifests for historical reasons
  121.  
  122. Z EQUR A0
  123. P EQUR A1
  124. G EQUR A2
  125. L EQUR A3
  126. B EQUR A4
  127. S EQUR A5
  128. R EQUR A6
  129.  
  130. * Offsets within the overlay table.
  131. SysBase EQU         4
  132.  
  133. OTMARK  EQU         0                   File position
  134. OTLEVL  EQU        12                   Level
  135. OTORD   EQU        16                   Ordinate
  136. OTIHNK  EQU        20                   Initial hunk for load
  137. OTRHNK  EQU        24                   Hunk containing symbol
  138. OTOFF   EQU        28                   Offset of symbol
  139. LIBWORD EQU        23456
  140.  
  141.  
  142. * A macro to call a LIBRARY routine:
  143. CALLS   MACRO
  144.         MOVEA.L    SysBase,A6
  145.         JSR        _LVO\1(A6)
  146.         ENDM
  147.  
  148. * Call a library, save and restore A6.
  149. CALL    MACRO      ; CALL <library_vector_offset>,<library_pointer>
  150.     MOVE.L       A6,-(SP)
  151.     MOVE.L       \2,A6
  152.         JSR        _LVO\1(A6)
  153.     MOVE.L     (SP)+,A6
  154.         ENDM
  155.  
  156.  
  157. * Now for the first word of the program.
  158.  
  159. FIRST   BRA.L NextModule
  160.  
  161. * This next word serves to identify the overlay
  162. * supervisor to 'unloader'.
  163.  
  164.         DC.L       $ABCD                Special value
  165.  
  166.  
  167. * The loader plants values in the next locations.
  168.  
  169. STREAM  DC.L       0                    Overlay input stream
  170. OVTAB   DC.L       0                    Overlay table (Machine address)
  171. HTAB    DC.L       0                    Hunk table    (BCPL address)
  172. GLBVEC  DC.L       0                    Global vector (Machine address)
  173.  
  174.  
  175. * Now the overlay entry points.
  176. * Two versions, one for small offsets and one for large
  177.  
  178.  
  179. OVENT   MACRO
  180.         ENTRY      OVLY0\1
  181. OVLY0\1 MOVEQ      #\1*8,D0             Overlay number times size of entry
  182.         BRA        OV1                  Branch to start of code
  183.         ENDM
  184.  
  185. OVENT2  MACRO
  186.         ENTRY      OVLY0\1
  187. OVLY0\1 MOVE.L     #\1*8,D0             Overlay number times size of entry
  188.         BRA        OV1                  Branch to start of code
  189.         ENDM
  190.  
  191.         OVENT      000
  192.         OVENT      001
  193.         OVENT      002
  194.         OVENT      003
  195.         OVENT      004
  196.         OVENT      005
  197.         OVENT      006
  198.         OVENT      007
  199.         OVENT      008
  200.         OVENT      009
  201.         OVENT      010
  202.         OVENT      011
  203.         OVENT      012
  204.         OVENT      013
  205.         OVENT      014
  206.         OVENT      015
  207.         OVENT2     016
  208.         OVENT2     017
  209.         OVENT2     018
  210.         OVENT2     019
  211.         OVENT2     020
  212.  
  213.    IFGT OVERLAYS-20
  214.         OVENT2     021
  215.         OVENT2     022
  216.         OVENT2     023
  217.         OVENT2     024
  218.         OVENT2     025
  219.         OVENT2     026
  220.         OVENT2     027
  221.         OVENT2     028
  222.         OVENT2     029
  223.         OVENT2     030
  224.         OVENT2     031
  225.         OVENT2     032
  226.         OVENT2     033
  227.         OVENT2     034
  228.         OVENT2     035
  229.         OVENT2     036
  230.         OVENT2     037
  231.         OVENT2     038
  232.         OVENT2     039
  233.         OVENT2     040
  234.    ENDC
  235.  
  236.    IFGT OVERLAYS-40
  237.         OVENT2     041
  238.         OVENT2     042
  239.         OVENT2     043
  240.         OVENT2     044
  241.         OVENT2     045
  242.         OVENT2     046
  243.         OVENT2     047
  244.         OVENT2     048
  245.         OVENT2     049
  246.         OVENT2     050
  247.         OVENT2     051
  248.         OVENT2     052
  249.         OVENT2     053
  250.         OVENT2     054
  251.         OVENT2     055
  252.         OVENT2     056
  253.         OVENT2     057
  254.         OVENT2     058
  255.         OVENT2     059
  256.         OVENT2     060
  257.    ENDC
  258.  
  259.  
  260. *
  261. * The main code of the program.
  262. *
  263.         CNOP       0,4
  264.         DC.L       LIBWORD
  265.         DC.B       7,'Overlay'
  266.  
  267. OV1     MOVEM.L    A2-A7/D2-D7,-(SP)
  268.         MOVE.L     OVTAB,L              Address of overlay table
  269.         MOVEA.L    L,B
  270.         ADD.L      (L),D0               Add value in first word of table
  271.         LSL.L      #2,D0
  272.         ADDA.L     D0,L                 Address of overlay entry
  273.         MOVE.L     OTLEVL(L),D0         Get required level
  274.         LSL.L      #2,D0
  275.         ADDA.L     D0,B                 Pointer to entry in ordinate table
  276.         MOVE.L     OTORD(L),D0          Get required ordinate
  277.         CMP.L      (B),D0               Compare with actual
  278.         BEQ        GOTSEG               Branch if no load required
  279.  
  280. * Now clear other entries in the ordinate table
  281.  
  282.         MOVE.L     D0,(B)+
  283. OV2     TST.L      (B)                  Table terminated by zero
  284.         BEQ.S      OV3
  285.         CLR.L      (B)+
  286.         BRA.S      OV2
  287. *
  288. OV3     MOVE.L     OTIHNK(L),D0         First hunk number of overlay segment
  289.         ADD.L      HTAB,D0
  290.         LSL.L      #2,D0                Address of entry in hunk table
  291.         MOVEA.L    D0,B
  292.         MOVE.L     -4(B),D0             Previous hunk
  293.         LSL.L      #2,D0
  294.         MOVEA.L    D0,R
  295.         CLR.L      (R)                  Clear link field in case of error
  296.  
  297. * Now free all the unwanted hunks ...
  298.  
  299. OV4     TST.L      (B)                  Scan is terminated by zero
  300.         BEQ        OV5
  301.         MOVE.L     (B)+,D1              Address as argument for 'freevec'
  302. FREEVEC   TST.L      D1
  303.           BEQ.S      FVC1             Return if V = 0
  304.           ASL.L      #2,D1            Back to mc address
  305.           MOVEA.L    D1,A1
  306.           MOVE.L     -(A1),D0         D0 = size
  307.           MOVE.L     D0,D1            Take copy
  308.           ANDI.L     #$FF000003,D1    Top byte and bottom 2 bits..
  309.           BNE        OVERR            .. must be clear to be valid length
  310.           CALL       FreeMem,SysBase
  311. FVC1      BRA.S      OV4              and again
  312.  
  313. * Now save CIS and point the file
  314.  
  315. OV5
  316.         LEA.L   LIBNAME(PC),A1
  317.         MOVEQ   #0,D0
  318.         CALL    OpenLibrary,SysBase
  319.         MOVE.L  D0,D7
  320.         BEQ.S   OVERR
  321.  
  322.         ADDA.L     #OTMARK,L            Address of file mark
  323.  
  324. * Now seek to the segment and load it.  If we fail (probably because
  325. * the user removed the disk, then retry until successful.  This has
  326. * the effect of continually reprompting the user to insert the disk
  327. * until he obeys.
  328.  
  329. OV_RETRY:
  330.         MOVE.L     STREAM,D1            New stream
  331.         MOVE.L     (L),D2
  332.         MOVEQ      #OFFSET_BEGINNING,D3 Offset to be from beginning
  333.         CALL       Seek,D7                 Call 'point(scb,mark,o)'
  334.         TST.L      D1                   Check result
  335.         BMI.S      OV_RETRY             Branch if error
  336.  
  337.     ; Now call the loader again:
  338.         MOVE.L     HTAB,D2              Hunk table (second parameter)
  339.         MOVEQ      #0,D1                Zero parameter for overlay
  340.         MOVE.L     STREAM,D3
  341.         CALL       LoadSeg,D7
  342.         TST.L      D1                   Check result
  343.         BMI.S      OV_RETRY             Branch if error
  344.  
  345. * Add new list to chain after previous one
  346.  
  347.         MOVE.L     D1,(R)               Add new chain
  348.  
  349. * Here when segment is in store: A3 holds table entry, A1 and D0 are free
  350.  
  351. GOTSEG  MOVE.L     OTRHNK(L),D0         Number of hunk containing symbol
  352.         ADD.L      HTAB,D0
  353.         LSL.L      #2,D0                Machine address of entry in hunk table
  354.         MOVEA.L    D0,B
  355.         MOVE.L     (B),D0               BCPL address of hunk
  356.         LSL.L      #2,D0
  357.         ADD.L      OTOFF(L),D0          Add in offset of symbol
  358.  
  359. * The address to jump to is now in D0. Place it in A1 and jump to it.
  360.         MOVEA.L    D0,A1
  361.         MOVEM.L    (SP)+,A2-A7/D2-D7     restore regs
  362.         JMP        (A1)                  Jump to new code
  363.  
  364. * Come here on an error during the overlay
  365.  
  366. OVERR 
  367.     MOVE.L #AN_BadOverlay,D7
  368.     CALLS  Alert
  369.         RTS
  370.  
  371. * The code here will cause us to jump to the next segment in the list,
  372. * which should be the intended entry point.
  373.  
  374. NextModule
  375.         LEA.L      FIRST,A3        Pointer to start of module
  376.         MOVE.L     -4(A3),D7            Next BCPL word ptr
  377.         ASL.L      #2,D7                MC ptr to next module
  378.         MOVE.L     D7,A3                Into address register
  379.         JMP        4(A3)                Tally ho!
  380.  
  381. * Data Area
  382.  
  383. LIBNAME DC.B       'dos.library',0
  384.         CNOP       0,4
  385.  
  386.  
  387.         END
  388.